package com.library.controller;

import com.library.dto.pojo.excelPo.BookExportInfo;
import com.library.dto.pojo.excelPo.BookImportInfo;
import com.library.entity.Book;
import com.library.service.IBookService;
import com.library.utils.excelUtils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Controller
@RequestMapping ( "/book" )
public class BookController {

    @Autowired
    IBookService bookService;

    @RequestMapping ( "/add" )
    public String add( Book book ) {

        book.setCurrentCount(book.getTotalCount());
        book.setPutdate(new Date());

        System.out.println(book);

        book.insert();

        return "redirect:/admin/bookmanager/1";
    }

    @RequestMapping ( "/del/{bookId}" )
    public String del( @PathVariable int bookId ) {

        Book book = new Book();
        book.deleteById(bookId);

        return "redirect:/admin/bookmanager/1";
    }

    @RequestMapping ( "/update" )
    public String update( Book book ) {

        //根据封装的对象，查到数据库中原本的记录（方便获得记录）
        Book old = book.selectById();
        //        System.out.println("更新的书籍:" + book);

        /*
         * 在表单中上传时间属性的时候，抛出400错误；目前没有解决
         * 所以在原本的表单中就没有提交时间属性
         *
         *
         * 但是如果用表单提交的记录更新的话，就会丢失原本的上架时间，
         * 所以在更新数据库之前，就通过查询原本记录，获得对应的上架时间
         * */
        book.setPutdate(old.getPutdate());
        book.updateById();

        return "redirect:/admin/bookmanager/1";
    }

    /*——————————————————————————————————————————————————————————————————————————————————————————*/

    /**
     * 读取(解析)Excel（允许多个 sheet）
     */
    //    @ResponseBody
    @RequestMapping ( value = "readExcel", method = RequestMethod.POST )
    public String readExcel( @RequestParam ( "file" ) MultipartFile excel ) {
        List<Object> objects = ExcelUtil.readExcel(excel, new BookImportInfo());

        bookService.insertBatchFromExcel(objects);

        return "redirect:/admin/bookmanager/1";
    }

    /**
     * 导出 Excel（一个 sheet）
     */
    @ResponseBody
    @RequestMapping ( value = "writeExcel", method = RequestMethod.GET )
    public void writeExcel( HttpServletResponse response ) throws IOException {

        //获得图书的所有记录列表
        List<BookExportInfo> list = bookService.getBookExcelList();

        String s = LocalDateTime.now().toString();
        String timeStamp = s.substring(0, s.indexOf("T"));
        String fileName = timeStamp + "图书信息";
        String sheetName = "图书信息";

        ExcelUtil.writeExcel(response, list, fileName, sheetName, new BookExportInfo());
    }

    /**
     * 导出 Excel（多个 sheet）
     */
    @ResponseBody
    @RequestMapping ( value = "writeExcelWithSheets", method = RequestMethod.GET )
    public void writeExcelWithSheets( HttpServletResponse response ) throws IOException {
        List<BookExportInfo> list = getList();
        String fileName = "一个 Excel 文件";
        String sheetName1 = "第一个 sheet";
        String sheetName2 = "第二个 sheet";
        String sheetName3 = "第三个 sheet";

        ExcelUtil
            .writeExcelWithSheets(response, list, fileName, sheetName1, new BookExportInfo())
            .write(list, sheetName2, new BookExportInfo())
            .write(list, sheetName3, new BookExportInfo())
            .finish();
    }

    /**
     * 作为导出的excel的临时数据
     *
     * @return
     */
    private List<BookExportInfo> getList() {
        List<BookExportInfo> list = new ArrayList<>();

        BookExportInfo bookExportInfo = new BookExportInfo();
        bookExportInfo.setBookId(1);
        bookExportInfo.setBookName("111");
        bookExportInfo.setIsbn("111");
        bookExportInfo.setTypeName("111");
        bookExportInfo.setAuthor("111");
        bookExportInfo.setTotalCount(110);
        bookExportInfo.setCurrentCount(110);
        bookExportInfo.setPublisher("111");
        bookExportInfo.setDescription("1111");
        bookExportInfo.setPrice(0.0D);
        bookExportInfo.setPutdate(new Date());
        bookExportInfo.setTypeId(0);
        bookExportInfo.setIsExitPDF(0);
        bookExportInfo.setPdfURl("");
        //        bookExportInfo.setAdminId(1);

        list.add(bookExportInfo);

        return list;

    }

    @RequestMapping ( "/demo" )
    public String demo() {
        return "admin/demo";
    }

}

